import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mansui_kechaung/pages/customRule/child/action.dart';
import 'package:mansui_kechaung/pages/customRule/child/history.dart';
import 'package:mansui_kechaung/pages/customRule/child/setting.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class CustomRulePage extends StatefulWidget {
  const CustomRulePage({super.key});

  @override
  State<CustomRulePage> createState() => _CustomRulePageState();
}

class _CustomRulePageState extends State<CustomRulePage> {
  final List tabBodies = [
    CustomRuleActionPage(),
    CustomRuleHistoryPage(),
    CustomRuleSettingPage(),
  ];
  final List<NavigationRailDestination> navItems = [
    NavigationRailDestination(
      icon: Icon(CupertinoIcons.home),
      label: Text("采集"),
    ),
    NavigationRailDestination(
      icon: Icon(CupertinoIcons.search),
      label: Text('记录'),
    ),
    NavigationRailDestination(
      icon: Icon(CupertinoIcons.settings),
      label: Text('设置'),
    ),
  ];

  int currentIndex = 0;
  var currentPage;

  @override
  void initState() {
    super.initState();
    currentPage = tabBodies[currentIndex];
    initLocal();
  }

  Future<void> initLocal() async {
    final prefs = await SharedPreferences.getInstance();
    var ip = prefs.getString("ip");
    var port = prefs.getString("port");
    var station = prefs.getString("station");
    if (ip == null) {
      prefs.setString("ip", "192.168.5.123");
    }
    if (port == null) {
      prefs.setString("port", "5005");
    }
    if (station == null) {
      prefs.setString("station", "New001");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(toolbarHeight: 0),
      body: Row(
        children: [
          // 侧边导航栏
          NavigationRail(
            extended: false,
            // backgroundColor: Theme.of(context).colorScheme.primaryContainer,
            labelType: NavigationRailLabelType.all,
            selectedIndex: currentIndex,
            onDestinationSelected: (index) {
              setState(() {
                currentIndex = index;
                currentPage = tabBodies[currentIndex];
              });
            },
            destinations: navItems,
          ),
          // 主内容区域
          Expanded(child: currentPage),
        ],
      ),
    );
  }
}
